package cn.community.system.web.controller;

import cn.community.system.query.MemberReportQuery;
import cn.community.system.service.IMemberReportService;
import cn.community.system.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dataMemberReport")
public class MemberReportController {

    @Autowired
    private IMemberReportService memberReportService;

    @RequestMapping("/activityReportList")
    // 统计个人报名的活动个数、活动总时长（根据时间段、【年/月/日】搜索统计）
    @RequiresPermissions(value = {"dataMemberReport/activityReportList","社团成员-活动统计列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") MemberReportQuery qo){
        model.addAttribute("page",memberReportService.selectAllActivityReport(qo));
        return "dataReport/member/list";
    }

    @RequestMapping("/memberReportListByBar") // 柱状图
    @RequiresPermissions(value = {"dataMemberReport/memberReportListByBar","社团成员-活动统计柱状图"},logical = Logical.OR)
    public String listByBar(Model model, @ModelAttribute("qo") MemberReportQuery qo){
        //获取分页结果集
        //PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        //List<Map> list = pageInfo.getList();
        //获取所有数据(不分页)
        List<Map> list = memberReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储y轴的两个数据
        ArrayList yList1 = new ArrayList();
        ArrayList yList2 = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList1.add(map.get("activityNumbers"));
            yList2.add(map.get("activityTimes"));
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        System.out.println(JSON.toJSONString(xList));
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList1",JSON.toJSONString(yList1));
        model.addAttribute("yList2",JSON.toJSONString(yList2));
        model.addAttribute("groupTypeName", MessageUtil.changMemberMsg(qo));
        return "dataReport/activityListByBar";
    }


    @RequestMapping("/dataMemberReportistByPie")  // 饼图-总个数
    @RequiresPermissions(value = {"dataMemberReport/dataMemberReportistByPie","社团成员-活动个数饼图"},logical = Logical.OR)
    public String listByPie(Model model, @ModelAttribute("qo") MemberReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = memberReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("activityNumbers"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMemberMsg(qo));//分组类型
        return "dataReport/activityListByPie";
    }

    @RequestMapping("/dataMemberReportistByPie2")  // 饼图-总时长
    @RequiresPermissions(value = {"dataMemberReport/dataMemberReportistByPie2","社团成员-活动时长饼图"},logical = Logical.OR)
    public String listByPie2(Model model, @ModelAttribute("qo") MemberReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = memberReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("activityTimes"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMemberMsg(qo));//分组类型
        return "dataReport/activityListByPie2";
    }
}
